<?
require_once("conn.php");
$m = $_REQUEST["m"];
$y = $_REQUEST["y"];
if($m == ""){
	$m = date("m");
}
if($y == ""){
	$y = date("Y");
}

?>
<table width="500">
	<tr>
		<td><a href="#" onclick="log_buy_report()">Buy Report</a></td>
		<td><a href="#" onclick="log_sell_report()">Sell Report</a></td>
		<td><a href="#" onclick="log_sum_report()">Summary Report</a></td>
		<td><a href="#" onclick="log_stock_report()">Stock Report</a></td>
	</tr>
	<tr>
		<td colspan="3">&nbsp;</td>
	</tr>
	<tr>
		<td colspan="3">Month : 
			<select id="log_month" onchange="update_Report_buy()">
				<option value="">&nbsp;</option>
			<?
				$sql = "select month(Buy_Date) as mon from buy Group by month(Buy_Date);";
				$qry = mysql_query($sql);
				while($rw = mysql_fetch_array($qry)){
					if($m == $rw["mon"]){
						$sel = "selected";
					}else{
						$sel = "";
					}
			?>
				<option value="<?=$rw["mon"]?>" <?=$sel?>><?=$rw["mon"]?></option>
			<?
			}
			?>
			</select>
			&nbsp;Year : 
			<select id="log_year" onchange="update_Report_buy()">
				<option value="">&nbsp;</option>
				<?
					$sql = "select year(Buy_Date) as yea from buy Group by year(Buy_Date);";
					$qry = mysql_query($sql);
					while($rw = mysql_fetch_array($qry)){
						if($y == $rw["yea"]){
							$sel = "selected";
						}else{
							$sel = "";
						}
				?>
					<option value="<?=$rw["yea"]?>" <?=$sel?>><?=$rw["yea"]?></option>
				<?
				}
				?>
			</select>
			<span align="right" style="float:right;" ><a href="logexcel.php?page=buy&m=<?=$m?>&y=<?=$y?>" target="_blank"><img src="images/excel_icon.jpg" width="25" border="0"></a></span>
		</td>
	</tr>
</table>
<table width="500" border="1" style="border-collapse:collapse">
	<tr>
		<td>Barcode</td>
		<td>Product Name</td>
		<td>Qty</td>
		<td>Buy Date</td>
		<td>Buy By</td>
	</tr>
<?

$reportSQL = "SELECT Buy_ID,buy.Barcode as Barcode,Product_Name,buy.Qty as Qty,Buy_Date,Buy_by FROM buy left join stock on buy.barcode=stock.barcode WHERE month(Buy_Date)='".$m."' AND year(Buy_Date)='".$y."' ORDER by Buy_ID desc;";
$query = mysql_query($reportSQL);
while($row = mysql_fetch_array($query)){
?>
	<tr>
		<td><?=$row["Barcode"]?></td>
		<td><?=($row["Product_Name"])?$row["Product_Name"]:"Deleted Product"?></td>
		<td><?=$row["Qty"]?></td>
		<td><?=$row["Buy_Date"]?></td>
		<td><?=$row["Buy_by"]?></td>
	</tr>
<?
}

?>
</table>